package com.roadjava.convert.demo03;

import com.roadjava.beans.dto.CarDTO;
import com.roadjava.beans.dto.PartDTO;
import com.roadjava.beans.vo.CarVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Mapper
public abstract class CarConvert03 {

    public static CarConvert03 INSTANCE = Mappers.getMapper(CarConvert03.class);

    public abstract CarVO dto2vo(CarDTO carDTO);

    /**
     * AfterMapping：表示让 mapstruct 在调用完自动转换的方法之后，会来自动调用本方法
     * MappingTarget：表示传来的 carVO 对象是已经赋值过的
     */
    @AfterMapping
    public void dto2voAfter(CarDTO carDTO, @MappingTarget CarVO carVO) {
        List<PartDTO> partDTOS = carDTO.getPartDTOS();
        boolean hasPart = !CollectionUtils.isEmpty(partDTOS);
        carVO.setHasPart(hasPart);
    }

}